package com.feng.repository.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.feng.entity.ChatHistory;
import com.feng.mapper.ChatHistoryMapper;
import com.feng.repository.ChatHistoryRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;

@RequiredArgsConstructor
@Component
public class ChatHistoryRepositoryImpl implements ChatHistoryRepository {

    private final ChatHistoryMapper chatHistoryMapper;

    @Override
    public void save(String type, String charId, String userId) {
        chatHistoryMapper.AUTO_INCREMENT();
        LambdaQueryWrapper<ChatHistory> wrapper = new LambdaQueryWrapper<>();
                wrapper.eq(ChatHistory::getChatId, charId);
                wrapper.eq(ChatHistory::getUserId, userId);
                wrapper.eq(ChatHistory::getType, type);
        Long count = chatHistoryMapper.selectCount(wrapper);
        if(count != 0){
            return;
        }
        chatHistoryMapper.insert(new ChatHistory(charId, userId, type));
    }

    @Override
    public List<String> getChatHistory(String type, String userId) {
        LambdaQueryWrapper<ChatHistory> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ChatHistory::getUserId, userId);
        wrapper.eq(ChatHistory::getType, type);
        List<ChatHistory> chatHistories = chatHistoryMapper.selectList(wrapper);
        return chatHistories.stream().map(c -> c.getChatId()).collect(Collectors.toList());
    }
}
